﻿Imports System.Object
Imports VSModel
Imports Cls_Common
Imports System.IO
Partial Class FrontEnd_Control_Users_Login
    Inherits System.Web.UI.UserControl
    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)


    'Protected Sub btnLogin_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLogin.Click


    '    If chkRemember.Checked Then
    '        If (Request.Browser.Cookies) Then
    '            'Check if the cookie with name FRONTENDLOGIN exist on user's machine
    '            If (Request.Cookies("FRONTENDLOGIN") Is Nothing) Then
    '                'Create a cookie with expiry of 30 days                    
    '                Response.Cookies("FRONTENDLOGIN").Expires = DateTime.Now.AddDays(30)
    '                Response.Cookies("FRONTENDLOGIN").Item("USERNAME") = txtUserName.Text
    '            Else
    '                Response.Cookies("FRONTENDLOGIN").Item("USERNAME") = txtUserName.Text
    '            End If
    '        End If
    '    End If

    '    'Login here
    '    'set session

    '    Using data As New VSEntities
    '        Try
    '            Dim strEncryptPass As String = Encrypt(txtPassword.Text)

    '            Dim p As User = (From q In data.Users Where q.UserName = txtUserName.Text.Trim And q.Password = strEncryptPass).SingleOrDefault
    '            If (Not p Is Nothing) AndAlso (p.IsActivated = True) Then
    '                Session("UserName") = txtUserName.Text
    '                Session("UserID") = p.UserId
    '                If p.ApproveVip = True Then
    '                    Session("UserVip") = p.Vip
    '                Else
    '                    Session("UserVip") = Nothing
    '                End If

    '                Session("UserCode") = p.Code
    '                Dim intUserId As Integer = p.UserId
    '                p.LastLogin = Now
    '                data.SaveChanges()
    '                'Lay menu tu UserMenu
    '                'Dim arrMenu() As Integer = (From a In data.UsersMenus Where a.UserId = p.UserId Select a.MenuId).ToArray
    '                'Session("MenuId") = arrMenu
    '                'Dim arrID() As Integer = (From t In data.UserRoles Where t.UserId = p.UserId Select t.RoleId).ToArray
    '                'If arrID.Length > 0 Thef
    '                '    Session("RoleID") = arrID
    '                '    Dim k = (From q In data.SysFuncRolesStatusPermissions Where arrID.Contains(q.RoleId) Select q.FunctionId).Distinct().ToArray
    '                '    Session("Function") = k
    '                '    If k.Length <= 0 Then
    '                '        Excute_Javascript("Alertbox('Bạn chưa được cấp quyền quản trị.');", Me.Page, True)
    '                '        Exit Sub
    '                '    End If

    '                'Else
    '                '    Excute_Javascript("Alertbox('Bạn chưa được cấp quyền quản trị.');", Me.Page, True)
    '                '    Exit Sub
    '                'End If
    '                'Excute_Javascript("alert('Đăng nhập thành công.');", Me.Page, True)
    '                Dim retURL As String = MainDomain & "index.html" 'IIf(Request("retURL") Is Nothing, MainDomain & "dang-nhap.html", Request("retURL"))
    '                Response.Redirect(retURL)



    '            Else
    '                'Excute_Javascript("alert('Tên đăng nhập hoặc mật khẩu chưa chính xác.');", Me.Page, True)
    '                lblError.Text = "Tên đăng nhập hoặc mật khẩu chưa chính xác. <br> Hãy nhập lại."
    '                'lblLoginFail.Visible = True
    '                'Dim retURL As String = MainDomain & "dang-nhap.html" 'IIf(Request("retURL") Is Nothing, MainDomain & "dang-nhap.html", Request("retURL"))
    '                'Response.Redirect(retURL)
    '            End If

    '        Catch ex As Exception
    '            log4net.Config.XmlConfigurator.Configure()
    '            log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
    '        End Try
    '    End Using
    'End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            hplRegist.NavigateUrl = MainDomain & "dang-ky.html"
            ' hidDomain.Value = MainDomain
            'Check if the browser support cookies     
            Dim script As ScriptManager = ScriptManager.GetCurrent(Me.Page)
            If Not script Is Nothing AndAlso script.IsInAsyncPostBack Then
                ScriptManager.RegisterStartupScript(Me.Page, Me.Page.GetType, "duyjs", "ajaxJquery()", True)
            Else
                Me.Page.ClientScript.RegisterStartupScript(Me.Page.GetType(), "duyjs", String.Concat("Sys.Application.add_load(function(){", "ajaxJquery()", "});"), True)
            End If

            If Request.Browser.Cookies Then
                If Request.Cookies("FRONTENDLOGIN") IsNot Nothing Then
                    txtUserName.Text = Request.Cookies("FRONTENDLOGIN")("USERNAME").ToString()

                End If
            End If
        End If
    End Sub


End Class
